package com.netease.api.post;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.framework.common.ext.util.HttpClientUtil;
import org.framework.common.ext.util.HttpClientUtil.RequestType;
import org.framework.common.ext.util.PropertiesUtil;
import org.framework.common.ext.util.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netease.api.util.EncodeUtil;

/**
 * @author Firrela
 * @time 2016/3/7.
 */
public class NIMPost {

    private static Logger logger = LoggerFactory.getLogger(NIMPost.class);
    
    private static String APP_KEY = null;
    
    private static String APP_SECRET = null;
    
    private static NIMPost instance;
    
    public static NIMPost factory(){
    	if(instance == null) instance = new NIMPost();
    	
    	PropertiesUtil properties = PropertiesUtil.loader("/config/properties/config.properties");
    	APP_KEY = properties.getProperty("APP_KEY");
    	APP_SECRET = properties.getProperty("APP_SECRET");
    	
    	return instance;
    }
    
    private List<NameValuePair> params;
    
    public NIMPost addParams(final String key, final Object value){
    	if(params == null) params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair(key, String.valueOf(value)));
    	return this;
    }
    
    /**
     * 调用云信接口.
     * @param url
     * @param entity
     * @return
     * @throws IOException
     * @author chanlong(陈龙)
     * @date 2016年9月20日 下午5:51:46
     */
	public NIMResponse postNIMServer(final String url) throws IOException {
    	String nonce = UUIDUtils.getUUID();
        String curtime = String.valueOf(System.currentTimeMillis() / 1000);
        String checksum = getCheckSum(nonce, curtime, APP_SECRET);

        logger.info("Nonce {} | CurlTime {} | CheckSum {}", new Object[]{nonce, curtime, checksum});
        
        String result = HttpClientUtil.request(RequestType.post, url, params)
  			  				 .addHead("AppKey", APP_KEY)
  			  				 .addHead("Nonce", nonce)
  			  				 .addHead("CurTime", curtime)
  			  				 .addHead("CheckSum", checksum)
  			  				 .addHead("charset", Consts.UTF_8.name())
  			  				 .addHead("Content-Type", HttpClientUtil.ApplicationType.FORM.value())
  			  				 .execute();
        System.out.println("postNIMServer: "+result);
        //Map<String, Object> map = new HashMap<String, Object>();
        /*String array[] = result.split("},");
        map.put("obj", (array[0].substring(7)+"}"));
        map.put("code",(Integer.valueOf(array[1].substring(7, 10))));*/
        return new NIMResponse(result);
    }
    
    /**
     * 调用云信接口.
     * @param url
     * @param entity
     * @return
     * @throws IOException
     * @author chanlong(陈龙)
     * @date 2016年9月20日 下午5:51:46
     */
	public NIMResponse postNIMServer(final String url, final List<NameValuePair> params) throws IOException {
    	String nonce = UUIDUtils.getUUID();
        String curtime = String.valueOf(System.currentTimeMillis() / 1000);
        String checksum = getCheckSum(nonce, curtime, APP_SECRET);

        logger.info("Nonce {} | CurlTime {} | CheckSum {}", new Object[]{nonce, curtime, checksum});
        
        String result = HttpClientUtil.request(RequestType.post, url, params)
  			  				 .addHead("AppKey", APP_KEY)
  			  				 .addHead("Nonce", nonce)
  			  				 .addHead("CurTime", curtime)
  			  				 .addHead("CheckSum", checksum)
  			  				 .addHead("charset", Consts.UTF_8.name())
  			  				 .addHead("Content-Type", HttpClientUtil.ApplicationType.FORM.value())
  			  				 .execute();
        System.out.println("postNIMServer:"+result);
        return new NIMResponse(result);
    }

    /*
     *  设置令牌
     */
    private static String getCheckSum(final String nonce, final String curtime, final String secret) {
        String plaintext = new StringBuilder(secret).append(nonce).append(curtime).toString();
        return EncodeUtil.encode(plaintext, "SHA1");
    }
}
